How to get all options in a drop-down list by Selenium WebDriver using C#?
Asked Answered
A

12

19

I'm new to both C# and Selenium WebDriver.

I know how to select/click on an option in a drop-down list, but I've a problem before that. Since the drop-down list is dynamically generated, I have to get all options/values from the list before running each case.

Is there anyone kindly tell me how to get all values/options from a drop-down list. I'm using IE and I didn't find any class which supports method to get values/options in Selenium.IE namespace for C#.

My example: A list contains several time zones:

<TD>
  <select name = "time_zone">
    <option value "-09:00"><script>timezone.Alaska</script></option>
    <option value "+00:00"><script>timezone.England</script></option>
    <option value "+02:00"><script>timezone.Greece</script></option>
    <option value "+05:30"><script>timezone.India</script></option>
  </select>
<TD>

This is a drop-down list in an IE page and how to get the dynamically generated time zone list?

My code:

IWebElement elem = driver.FindElement(By.XPath("//select[@name='time_zone']"));
List<IWebElement> options = elem.FindElements(By.TagName("option"));

C# just pops an Error: Cannot implicitly covert type 'OpenQA.Selenium.IWebElement' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?).

thanks.

Adventuresome answered 5/3, 2012 at 7:9 Comment(0)
F
21

You can try using the WebDriver.Support SelectElement found in OpenQA.Selenium.Support.UI.Selected namespace to access the option list of a select list:

IWebElement elem = driver.FindElement(By.XPath("//select[@name='time_zone']"));

SelectElement selectList = new SelectElement(elem);
IList<IWebElement> options = selectList.Options;

You can then access each option as an IWebElement, such as:

IWebElement firstOption = options[0];
Assert.AreEqual(firstOption.GetAttribute("value"), "-09:00");
Factitious answered 8/3, 2012 at 4:5 Comment(0)
B
9
Select select = new Select(driver.findElement(By.id("searchDropdownBox")));

select.getOptions();//will get all options as List<WebElement>
Breadroot answered 7/2, 2014 at 14:36 Comment(0)
R
7

Make sure you reference the WebDriver.Support.dll assembly to gain access to the OpenQA.Selenium.Support.UI.SelectElement dropdown helper class. See this thread for additional details.

Edit: In this screenshot, you can see that I can get the options just fine. Is IE opening up when you create a new InternetExplorerDriver? Screenshot

Rhinelandpalatinate answered 5/3, 2012 at 8:8 Comment(4)
I've already added WebDriver.dll and WebDriver.Support.dll into the reference.Adventuresome
anyway, thank you all the same. I'm still looking for solutions.Adventuresome
Your code should work if you use implicit typing: var options = elem.FindElements(By.TagName("option"));Rhinelandpalatinate
jmac, thank you. I tried, but it still returns the type of the node rather than the value/text of the option. I also tried: var tz = elem.FindElment(xx).Text.ToString(); It returns " ", just a space. Do you know why? thx.Adventuresome
H
3

Here is code in Java to get all options in dropdown list.

WebElement sel = myD.findElement(By.name("dropdown_name"));
List<WebElement> lists = sel.findElements(By.tagName("option"));
    for(WebElement element: lists)  
    {
        String var2 = tdElement.getText();
        System.out.println(var2);
    }

Hope it may helpful to someone.

Halfbeak answered 22/11, 2012 at 11:2 Comment(0)
T
2

You can use selenium.Support to use the SelectElement class, this class have a property "Options" that is what you are looking for, I created an extension method to convert your web element to a select element

public static SelectElement AsDropDown(this IWebElement webElement)
{
    return new SelectElement(webElement);
}

then you could use it like this

var elem = driver.FindElement(By.XPath("//select[@name='time_zone']"));
var options = elem.AsDropDown().Options
Twila answered 18/3, 2016 at 17:4 Comment(0)
W
1

Use IList<IWebElement> instead of List<IWebElement>.

For instance:

IList<IWebElement> options = elem.FindElements(By.TagName("option"));
foreach (IWebElement option in options)
{
    Console.WriteLine(option.Text);
}
Wong answered 8/3, 2012 at 9:13 Comment(0)
H
0

It seems to be a cast exception. Can you try converting your result to a list i.e. elem.findElements(xx).toList ?

Helladic answered 5/3, 2012 at 11:27 Comment(2)
Yeah, I tried in this way. But when I printed the elements of the List, all element of List[i] is "OpenQA.Selenium.Remote.RemoteWebElement", which is the type but not the value.Adventuresome
Thank you all the same. I found a method in Selenium namespace called "GetSelectOptions", but I didn't how to use it with class IWebElement or InternetExplorerWebDriver.Adventuresome
C
0
To get all the dropdown values you can use List.

List<string> lstDropDownValues = new List<string>();
int iValuescount = driver.FindElement(By.Xpath("\html\....\select\option"))

for(int ivalue = 1;ivalue<=iValuescount;ivalue++)
 {
  string strValue = driver.FindElement(By.Xpath("\html\....\select\option["+ ivalue +"]"));
  lstDropDownValues.Add(strValue); 
 }
Civilization answered 2/5, 2015 at 7:28 Comment(0)
J
0
WebElement drop_down =driver.findElement(By.id("Category"));
Select se = new Select(drop_down);
for(int i=0 ;i<se.getOptions().size(); i++)
System.out.println(se.getOptions().get(i).getAttribute("value"));
Jopa answered 19/2, 2016 at 13:3 Comment(0)
K
0
 WebElement element = driver.findElement(By.id("inst_state"));
        Select s = new Select(element);
        List <WebElement> elementcount = s.getOptions();

        System.out.println(elementcount.size());
        for(int i=0 ;i<elementcount.size();i++)
        {
            String value = elementcount.get(i).getText();
            System.out.println(value);

        }
Khartoum answered 16/3, 2016 at 23:37 Comment(0)
S
0

To get all options in a drop-down list by Selenium WebDriver C#:

SelectElement TimeZoneSelect = new SelectElement(driver.FindElement(By.Name("time_zone")));
IList<IWebElement> ElementCount = TimeZoneSelect.Options;
int ItemSize = ElementCount.Count;

for (int i = 0; i < ItemSize; i++)
{
    String ItemValue = ElementCount.ElementAt(i).Text;
    Console.WriteLine(ItemValue);
}
Shrine answered 22/5, 2017 at 12:35 Comment(0)
M
0

You need to pass the desired select webelement here and you can use getOptions() that helps you to get all the options of the drop-down. Now you can get all the elements one-by-one using loop and you can print it on the console.

Select s = new Select(driver.findElement(By.xpath("<xpath>")));
// getting the list of options in the dropdown with getOptions()
List <WebElement> op = s.getOptions();
int size = op.size();
for(int i =0; i<size ; i++)
{
   String options = op.get(i).getText();
   System.out.println(options);
}
Made answered 9/12, 2021 at 5:31 Comment(2)
Code-only answer are not suitable. Please consider adding some explanation on how and why your code should solve the problem.Lefkowitz
@Lefkowitz done now. Thanks for the suggestion :)Made

© 2022 - 2024 — McMap. All rights reserved.